home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 2001 January / CT_SW0101.ISO / mac / software / office / text / texedit.sit / TE+ 4.1.1Dƒ / Englische Originaldokumentation / W.T.H.I. AppleScript? < prev    next >
Text File  |  2000-07-10  |  16KB  |  403 lines

  1.  
  2.                What the Heck is AppleScript?
  3.  
  4. AppleScript is an English-like programming language that allows you to use Apple Events to control applications.
  5.  
  6. Great.  What’s an Apple Event?
  7.  
  8. With the advent of System 7, Apple introduced a powerful new way for applications to communicate with each other.  Each Apple Event represents a single “message” in this digital dialog.
  9.  
  10. For instance, when you double-clicked this document, the Finder actually sent an “open document” Apple Event to Tex-Edit.  Tex-Edit then recognized the event and displayed this document for you.
  11.  
  12. What’s the big deal?
  13.  
  14. Well, Apple Events allow “scriptable” applications to communicate at a very intimate level.  Unlike macros, Apple Events bypass the user interface and are quite efficient.
  15.  
  16. AppleScript puts the power of these Apple Events in the hands of the ordinary user.  It’s just another insanely great advantage that we Mac users enjoy.
  17.  
  18. What does it mean to say an application is “scriptable?”
  19.  
  20. Scriptability implies that the programmer has given AppleScript access to major portions of the application’s inner workings.
  21.  
  22. If AppleScript is so great, why aren’t all applications scriptable?
  23.  
  24. Changing an existing application to make it scriptable involves an effort that I like to refer to as “non-trivial.” Luckily, most significant applications are now scriptable.
  25.  
  26. Do I have to learn to program to use AppleScript?
  27.  
  28. Well, it kind of depends on what you mean by “programming.” The AppleScript dialect is very forgiving and looks a lot like ordinary English.
  29.  
  30. No you don’t understand.  I REALLY hate programming!
  31.  
  32. You’re in luck!
  33.  
  34. Tex-Edit, like many other scriptable applications, is also “recordable.” Apple’s Script Editor can record your actions as you use Tex-Edit.  The resulting “script” (program) can be saved and re-played later.
  35.  
  36. So, AppleScript isn’t just for nerds?
  37.  
  38. The real nerds are the folks who waste time doing repetitive computing chores, instead of letting their Mac do the work.
  39.  
  40. Okay, show me an example of an AppleScript program.
  41.  
  42. Well, let’s see.  How about if we create a “style sheet” program which sets the font of the first window to 12 point Monaco (ick!) and then sets the size of the first character of every paragraph to 24 points.
  43.  
  44. Just how much programming work would that require?
  45.  
  46. You just saw it.
  47.  
  48. Huh?
  49.  
  50. Well, actually the program might look something like this:
  51.  
  52.    tell first window of application "Tex-Edit Plus"
  53.       set the font of the contents to "Monaco"
  54.       set the size of the contents to 12
  55.       set the size of the first character of every paragraph to 24
  56.    end tell
  57.  
  58. Not bad! What does the first line do?
  59.  
  60. It’s just a shortcut.  Notice how the “tell” and “end tell” lines bracket all the commands.  This saves us a little typing, otherwise the program would read:
  61.  
  62.    set the font of the contents
  63.       of the first window of application "Tex-Edit Plus" to "Monaco"
  64.    set the size of the contents
  65.       of the first window of application "Tex-Edit Plus" to 12
  66.    set the size of the first character of every paragraph
  67.       of the first window of application "Tex-Edit Plus" to 24
  68.  
  69. But, doesn’t every word have to be in just the right place?
  70.  
  71. AppleScript is a very forgiving programming language.  For example, these two programs do the same thing:
  72.  
  73.    set the size of the first word of the third document to ten
  74.    set size of word 1 of window 3 to 10
  75.  
  76. Okay already! How do I add scripts to Tex-Edit’s  menu?
  77.  
  78. When Script Editor saves a script as a “compiled script,” it creates a script document which can be placed in Tex-Edit’s “Scripts” folder.  When Tex-Edit launches, it adds everything in this folder to its Scripts menu, providing easy access to your favorite scripts.
  79.  
  80. I don’t see a  menu.
  81.  
  82. Either there is no “Scripts” folder in the same folder as Tex-Edit or you have a System version earlier than 7.5.
  83.  
  84. Well, I have System 7.5, but I meticulously removed lots of stuff in the Extensions folder to make my system more stable.
  85.  
  86. Don’t do that!  You probably managed to remove lots of optimized code patches and bug fixes.  It’s not worth the tiny amount of disk space you saved.
  87.  
  88. I want to install AppleScript.  Where can I find it?
  89.  
  90. AppleScript and Script Editor are included free with all Systems since 7.5.  If you have an earlier version of System 7, you can download the latest AppleScript installer from Apple’s web site:
  91. <http://applescript.apple.com/>
  92.  
  93. How can I learn more about writing scripts?
  94.  
  95. First, visit Doug Adams’ AppleScript archive.  It is devoted specifically to scripting Tex-Edit and contains a guide for beginners:
  96. <http://www.malcolmadams.com/te/>
  97.  
  98. You should also subscribe to the MACSCRPT mailing list.  It is very active and includes lots of helpful information from lots of AppleScript users.  To subscribe to MACSCRPT:
  99. send email to: <mailto:LISTSERV@dartmouth.edu>
  100. with subject: “subscribe”
  101. and message: “subscribe macscrpt <your full name here>”.
  102.  
  103. If you want to appreciate the full power of AppleScript, I would recommend Danny Goodman’s “AppleScript Handbook.” It’s an excellent source of information, especially for beginners.
  104.  
  105. Finally, just experiment with Script Editor’s “record” function to see examples of the syntax Tex-Edit uses when sending messages to itself.
  106.  
  107. But, I want to write a script NOW!
  108.  
  109. Okay, first launch Tex-Edit and then launch Script Editor (which should be located in the Apple menu).  To execute an AppleScript command, simply type the command into the Script Editor window, then press the “run” button:
  110.  
  111.      
  112.  
  113. In an AppleScript program, each line is a separate command.  Usually the commands are bracketed by “tell” and “end tell” statements, as explained above.  When the program runs, command lines are executed sequentially from top to bottom.
  114.  
  115. Try typing in and running the following program.  (Lines which start with a double hyphen are optional comments.) 
  116.  
  117. 
  118.  
  119. Once you get a program to do what you want it to do, save it as a “compiled script” document inside of Tex-Edit’s “Scripts” folder.
  120.  
  121. What commands does Tex-Edit understand?
  122.  
  123. Every scriptable application contains a “dictionary” which can be explored using Script Editor.  I have included a brief synopsis of Tex-Edit’s dictionary below, but I would recommend using Script Editor to print out the real thing.
  124.  
  125. How do I interpret what’s in the dictionary?
  126.  
  127. The dictionary is divided up into chapters (or “suites”)--Required Suite, Standard Suite, Text Suite, etc.  Each suite heading is followed by a list of commands and objects (or “classes”).
  128.  
  129. The dictionary defines every AppleScript word that is recognized by Tex-Edit, but it does not explain all the different possible usages.  Try using Script Editor’s “record” button to see examples of acceptable syntax.
  130.  
  131. What’s an object?
  132.  
  133. In the program:
  134.  
  135.    close window 1 of application "Tex-Edit Plus"
  136.  
  137. the verb “close” is the command and “window 1 of application Tex-Edit Plus” is the object.
  138.  
  139. Many of the objects have “properties.” You can use the Get and Set commands (described below) to extract an object’s property and change it to something different.  Notice how the “of” keyword can chain together a list of objects, allowing you to unambiguously specify any given object.
  140.  
  141.    tell application "Tex-Edit Plus"
  142.       get name of window 1
  143.       set size of character 1 of word 1 of window "untitled" to 24
  144.    end tell
  145.  
  146. In the first command, “name” is a property of the object “window 1 of application Tex-Edit Plus.”  In the second command, “size” is a property of the object “character 1 of word 1 of window "untitled" of application Tex-Edit Plus.”
  147.  
  148. _____________________________
  149.  
  150.  
  151. Commonly-used commands:
  152.  
  153. Here are some of the basic commands understood by most applications along with some simple examples of usage.  Notice that many commands correspond to standard menu items.  Examine each example to see the basic syntax.  Of course, the best way to understand the commands is to actually type them in and try them out.
  154.  
  155. activate
  156.    Brings an application to the front.
  157.    ex:
  158.       activate application "Tex-Edit Plus"
  159.  
  160. quit
  161.    Quits an application.
  162.    ex:
  163.       quit application "Tex-Edit Plus"
  164.       quit application "Tex-Edit Plus" saving no
  165.  
  166. open
  167.    Opens a document file.
  168.    ex:
  169.       tell application "Tex-Edit Plus"
  170.          open file "MacHD:Saving the World"
  171.       end tell
  172.  
  173. print
  174.    Prints a document or window.
  175.    ex:
  176.       print every window of application "Tex-Edit Plus"
  177.       print the last window of application "Tex-Edit Plus"
  178.  
  179. close
  180.    Closes the given window.
  181.    ex:
  182.       close every window of application "Tex-Edit Plus"
  183.       close windows 2 through 4 of application "Tex-Edit Plus"
  184.  
  185. count
  186.    Returns the number of items.
  187.    ex:
  188.       count the words of window 2 of application "Tex-Edit Plus"
  189.       count the windows of application "Tex-Edit Plus"
  190.  
  191. delete
  192.    Deletes the given text.
  193.    ex:
  194.       tell window 1 of application "Tex-Edit Plus"
  195.          delete the first word
  196.          delete every character whose color is red
  197.       end tell
  198.  
  199. get
  200.    Gets whatever information you ask for.  Get and Set are used a lot!
  201.    ex:
  202.       tell application "Tex-Edit Plus"
  203.          get the font of the third word of window 1
  204.          get the position of the first window
  205.       end tell
  206.  
  207. set
  208.    Changes the value of an object’s property.
  209.    ex:
  210.       tell window 1 of application "Tex-Edit Plus"
  211.          set the color of the last word to red
  212.          set the name to "Fun With AppleScript"
  213.       end tell
  214.  
  215. make
  216.    Creates a new window or text item.
  217.    ex:
  218.       tell application "Tex-Edit Plus"
  219.          make new window behind window 1
  220.          make new line at end of text of window 1 with data "Dear Occupant:"
  221.       end tell
  222.  
  223. save
  224.    Saves the window to disk.
  225.    ex:
  226.       tell application "Tex-Edit Plus"
  227.          save the first window in "MacHD:Letter to MicroFlaccid"
  228.       end tell
  229.  
  230. select
  231.    Selects the desired text or window.
  232.    ex:
  233.       tell application "Tex-Edit Plus"
  234.          select the last document of application "Tex-Edit Plus"
  235.          select text from character 1 to character 60000 of window 2
  236.       end tell
  237.  
  238. undo/copy/cut/paste/revert
  239.    Executes the familiar menu command.
  240.    ex:
  241.       tell application "Tex-Edit Plus"
  242.          copy
  243.          copy unstyled
  244.       end tell
  245.  
  246.  
  247. Other commands (specific to Tex-Edit):
  248.  
  249. add line endings
  250.    Adds CRs and LFs (carriage returns and line feed characters) to the text.
  251.    ex:
  252.       tell selection of application "Tex-Edit Plus"
  253.          add line endings appending crlf
  254.       end tell
  255.  
  256. block format
  257.    Converts the given text into an indented block.
  258.    ex:
  259.       tell the first window of application "Tex-Edit Plus"
  260.          block format the first paragraph with line header ">  "
  261.          block format the last paragraph with line length 45
  262.       end tell
  263.  
  264. change case
  265.    Changes the case of the given text.  (Note the alternate method using the “capitalization” property.)
  266.    ex:
  267.       tell the middle window of application "Tex-Edit Plus"
  268.          change case of line 3 into uppercase
  269.          set capitalization of line 3 to uppercase
  270.       end tell
  271.  
  272. search
  273.    Searches for the given text.
  274.    ex:
  275.       search the last window looking for "important item"
  276.  
  277. replace
  278.    Replaces all occurrences of one string with another.
  279.    ex:
  280.       tell application "Tex-Edit Plus"
  281.          replace window 1 looking for "Copland" replacing with "Allegro"
  282.          replace the last window replacing multiple spaces with tab
  283.       end tell
  284.  
  285. smarten/stupefy
  286.    Transforms to/from typographical characters.
  287.    ex:
  288.       smarten first window of application "Tex-Edit Plus" converting quotes
  289.  
  290. speak
  291.    Read the given text aloud.
  292.    ex:
  293.       speak "hello"
  294.       speak first paragraph of the third window of application "Tex-Edit Plus"
  295.  
  296. strip
  297.    Remove the specified characters from the text.
  298.    ex:
  299.       tell application "Tex-Edit Plus"
  300.          strip the first window removing leading spaces
  301.       end tell
  302.  
  303. strip line endings
  304.    Remove commonly used line ending characters.
  305.    ex:
  306.       tell window 2 of application "Tex-Edit Plus"
  307.          strip line endings removing cr
  308.          strip line endings of selection removing crlf
  309.       end tell
  310.  
  311.  
  312. Objects:
  313.  
  314. Many objects can be combined with the preceding commands into meaningful statements, however some combinations are not allowed.
  315.  
  316. For example you can:
  317.    close window 1 of application "Tex-Edit Plus"
  318.    print window 1 of application "Tex-Edit Plus"
  319.  
  320. but you can’t:
  321.    delete window 1 of application "Tex-Edit Plus"
  322.    quit window 1 of application "Tex-Edit Plus"
  323.  
  324. “Trial and error” is a good way to get started.  It is also helpful to use Script Editor’s “record” function to watch what happens as you use Tex-Edit.  Note the use of the Get and Set commands to access the properties of each object.  Here are a few of the available objects along with a few of their properties:
  325.  
  326. application
  327.    This is Tex-Edit itself.  Its properties are “global.”
  328.  
  329.    sample properties:
  330.       selection:  The current text selection in the front window.
  331.       search string:  The string used in the Find dialog.
  332.       speech enabled:  Used to turn on/off the Speech Manager functions.
  333.  
  334.    ex:
  335.       tell application "Tex-Edit Plus"
  336.          set font of selection to "Monaco"
  337.          set search string to "interesting stuff"
  338.          set speech enabled to true
  339.          get selection
  340.       end tell
  341.  
  342. window
  343.    This is synonymous with “document.”
  344.  
  345.    sample properties:
  346.       name:  The title of the document.
  347.       position:  The coordinates of the top left corner.
  348.       justification:  Used to set text justification in the window.
  349.  
  350.    ex:
  351.       tell application "Tex-Edit Plus"
  352.          set the position of some window to {125, 250}
  353.          set justification of every document to center
  354.          get name of window 1
  355.       end tell
  356.  
  357. character
  358.    This is a single character in a window.
  359.  
  360.    sample properties:
  361.       font:  The font face of the character.
  362.       size:  Font size of the character.
  363.       style:  Styles of the character (bold, italic, etc.).
  364.       color:  Color of the character.
  365.  
  366.    ex:
  367.       tell window 3 of application "Tex-Edit Plus"
  368.          set size of (text from character 1 to word 10) to 24
  369.          set the style of character 1 to {bold, italic, underline}
  370.          set color of the last character to red
  371.          get the font of character 1
  372.       end tell
  373.  
  374. word-line-paragraph-text
  375.    These objects share all the properties of character.
  376.  
  377.    ex:
  378.       tell window 1 of application "Tex-Edit Plus"
  379.          set the color of paragraph 2 to blue
  380.          set size of line 1 to 24
  381.          set style of (text from word 2 to word 4) to bold
  382.          get the font of word 1
  383.       end tell
  384.  
  385. _____________________________
  386.  
  387.  
  388. AppleScript seems so simple.  Are there any limits?
  389.  
  390. This document barely brushes the surface of the possibilities.  My favorite trick is to use voice-actuated “style sheet” scripts to switch styles as I type.
  391.  
  392. The sample scripts in Tex-Edit’s “Scripts” folder will help you get started.  Feel free to examine them and change them as much as you wish.
  393.  
  394. Where can I find more sample scripts?
  395.  
  396. Doug Adams maintains an archive of hints, tips and AppleScript examples at:
  397. <http://www.malcolmadams.com/te/>
  398.  
  399. In addition, there are hundreds of useful scripts located all over the web, available to anyone with a modem and a phone jack.  Be sure to check out the numerous AppleScript extensions (OSAX) that add even more power.
  400.  
  401. So, what are you waiting for?  You have nothing to lose but your tired fingers.
  402.  
  403. (Document Revised 10/11/99)